package org.fuxia.im.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.text.ParseException;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import org.fuxia.im.common.ClientConst;
import org.fuxia.im.common.ClientContext;
import org.fuxia.im.common.CommonDialog;
import org.fuxia.im.core.ECMessageType;
import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.socket.impl.ECNioClient;
import org.fuxia.im.utils.IdCard;
import org.tony.common.ui.CommonFrame;

public class Regist extends CommonFrame
{
	private IdCard idValidator ;

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private JPanel				contentPane;
	private JTextField			nickname;
	private JPasswordField		password;
	private JPasswordField		confirmPwd;
	private JTextField			birth;
	private JTextField			address;
	private JTextField			IC;

	private ButtonGroup bg ;
	private JRadioButton boy ;
	
	/**
	 * Create the frame.
	 */
	public Regist()
	{
		super();
		idValidator = new IdCard() ;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;
		setResizable(false);
		setBounds(100, 100, 460, 323);
		setTitle("注册账号");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContent(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel nicknameLab = new JLabel("昵  称：");
		nicknameLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		nicknameLab.setHorizontalAlignment(SwingConstants.RIGHT);
		nicknameLab.setBounds(44, 25, 75, 15);
		panel.add(nicknameLab);

		final JLabel nicknametip = new JLabel("*");
		nicknametip.setForeground(Color.RED);
		nicknametip.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		nicknametip.setBounds(311, 24, 123, 18);
		panel.add(nicknametip);

		nickname = new JTextField();
		nickname.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e)
			{
				nicknametip.setIcon(new ImageIcon(Regist.class.getResource("")));
				nicknametip.setText("*");
			}
		});

		nickname.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		nickname.setBounds(134, 20, 167, 25);
		panel.add(nickname);
		nickname.setColumns(10);

		JLabel pwdLab = new JLabel("密  码：");
		pwdLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		pwdLab.setHorizontalAlignment(SwingConstants.RIGHT);
		pwdLab.setBounds(44, 55, 75, 15);
		panel.add(pwdLab);

		JLabel conPwdLab = new JLabel("确认密码：");
		conPwdLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		conPwdLab.setHorizontalAlignment(SwingConstants.RIGHT);
		conPwdLab.setBounds(44, 85, 75, 15);
		panel.add(conPwdLab);

		final JLabel passwordtip = new JLabel("*");
		passwordtip.setForeground(Color.RED);
		passwordtip.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		passwordtip.setBounds(311, 55, 123, 18);
		panel.add(passwordtip);

		password = new JPasswordField();
		password.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e)
			{
				passwordtip.setIcon(new ImageIcon(Regist.class.getResource("")));
				passwordtip.setText("*");
			}
		});
		password.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		password.setBounds(134, 50, 167, 25);
		panel.add(password);

		final JLabel repwdtip = new JLabel("*");
		repwdtip.setForeground(Color.RED);
		repwdtip.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		repwdtip.setBounds(311, 85, 123, 18);
		panel.add(repwdtip);

		confirmPwd = new JPasswordField();
		confirmPwd.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e)
			{
				repwdtip.setIcon(new ImageIcon(Regist.class.getResource("")));
				repwdtip.setText("*");
			}
		});
		confirmPwd.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		confirmPwd.setBounds(134, 80, 167, 25);
		panel.add(confirmPwd);

		JLabel ICLab = new JLabel("身份证：");
		ICLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		ICLab.setHorizontalAlignment(SwingConstants.RIGHT);
		ICLab.setBounds(44, 113, 75, 15);
		panel.add(ICLab);

		final JLabel ictip = new JLabel("*");
		ictip.setForeground(Color.RED);
		ictip.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		ictip.setBounds(311, 113, 113, 15);
		panel.add(ictip);

		IC = new JTextField();
		IC.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e)
			{
				ictip.setIcon(new ImageIcon(Regist.class.getResource("")));
				ictip.setText("*");
			}
		});
		IC.setBounds(134, 111, 167, 25);
		panel.add(IC);
		IC.setColumns(10);

		JLabel sexLab = new JLabel("性  别：");
		sexLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		sexLab.setHorizontalAlignment(SwingConstants.RIGHT);
		sexLab.setBounds(44, 142, 75, 15);
		panel.add(sexLab);

		boy = new JRadioButton("男");
		boy.setSelected(true);
		boy.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		boy.setBounds(130, 138, 55, 23);

		JRadioButton girl = new JRadioButton("女");
		girl.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		girl.setBounds(187, 138, 55, 23);

		bg = new ButtonGroup();// 按三个单选按钮划分到一个按钮组中
		bg.add(boy);
		bg.add(girl);
		panel.add(boy);
		panel.add(girl);

		JLabel birthLab = new JLabel("生  日：");
		birthLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		birthLab.setHorizontalAlignment(SwingConstants.RIGHT);
		birthLab.setBounds(44, 173, 75, 15);
		panel.add(birthLab);

		birth = new JTextField();
		birth.setBounds(134, 168, 167, 25);
		panel.add(birth);
		birth.setColumns(10);

		JLabel addressLab = new JLabel("地  址：");
		addressLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		addressLab.setHorizontalAlignment(SwingConstants.RIGHT);
		addressLab.setBounds(44, 203, 75, 15);
		panel.add(addressLab);

		address = new JTextField();
		address.setBounds(134, 198, 250, 25);
		panel.add(address);
		address.setColumns(10);

		JButton regist = new JButton("注册");
		regist.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{

				if ("".equals(nickname.getText()) || nickname.getText() == null)
				{
					nicknametip.setIcon(new ImageIcon(Regist.class.getResource("/images/problem.gif")));
					nicknametip.setText("请输入昵称！");
					return;
				}
				if ("".equals(new String(password.getPassword())) || new String(password.getPassword()) == null)
				{
					passwordtip.setIcon(new ImageIcon(Regist.class.getResource("/images/problem.gif")));
					passwordtip.setText("请输入密码！");
					return;
				}
				if ("".equals(new String(confirmPwd.getPassword())) || new String(confirmPwd.getPassword()) == null)
				{
					repwdtip.setIcon(new ImageIcon(Regist.class.getResource("/images/problem.gif")));
					repwdtip.setText("请输入确认密码！");
					return;
				}
				if (!new String(password.getPassword()).equals(new String(confirmPwd.getPassword())))
				{
					repwdtip.setIcon(new ImageIcon(Regist.class.getResource("/images/problem.gif")));
					repwdtip.setText("两次密码不一致！");
					return;
				}
				if ("".equals(IC.getText()) || IC.getText() == null)
				{
					ictip.setIcon(new ImageIcon(Regist.class.getResource("/images/problem.gif")));
					ictip.setText("请输入身份证号！");
					return;
				}
				Map<String, Object> vr;
				try {
					vr = idValidator.IDCardValidate(IC.getText());
					boolean val_result = (Boolean)vr.get(IdCard.VAL_RESULT) ;
					if(!val_result) {
						if(vr.get(IdCard.ERROR_CODE) == null) {
							CommonDialog.showErrorMessageDialog(Regist.this, vr.get(IdCard.ERROR_INFO).toString()) ;
							return ;
						}
					}
				} catch (ParseException e1) {
					CommonDialog.showErrorMessageDialog(Regist.this, "身份证输入错误") ;
					return ;
				}
				

				contentPane.setEnabled(false);
				ECPacket packet = new ECPacket();
				packet.addParam("nickName", nickname.getText());
				packet.addParam("password", new String(password.getPassword()));
				packet.addParam("birthDay", birth.getText());
				packet.addParam("address", address.getText());
				packet.addParam("idCard", IC.getText());
				packet.addParam("sex", boy.isSelected()?"1":"0");
				packet.setMessageType(ECMessageType.REG_USER.name());

				
				ECNioClient client = ClientContext.getContext().getClient();
				if (client != null)
				{
					try
					{
						ClientContext.putValue(ClientConst.CURRENT_REQUESTOR,
								getThis());
						JDialog dia = CommonDialog.createModelWaitDialog(getThis());
						ClientContext.putValue(
								ClientConst.CURRENT_WAITING_DIALOG, dia);
						client.sendMessage(packet);
						dia.setVisible(true);
						new Timer().schedule(new TimerTask() {
							public void run() {
								Object o = ClientContext.removeKey(ClientConst.CURRENT_WAITING_DIALOG);
								if(o != null) {
									((JDialog)o).dispose() ;
								}
							}
						}, 20000) ;
					}
					catch (Exception e1)
					{
						JOptionPane.showMessageDialog(getThis(), "请检查你的网路连接状态",
								"提示信息", JOptionPane.ERROR_MESSAGE);
					}
				}
				else
				{
					JOptionPane.showMessageDialog(getThis(), "请检查你的网路连接状态",
							"提示信息", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		regist.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		regist.setBounds(134, 242, 75, 23);
		panel.add(regist);

		JButton cancel = new JButton("取消");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				setVisible(false);
				dispose();
				ClientContext.removeKey(ClientConst.REG_WINDOW);
			}
		});
		cancel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		cancel.setBounds(219, 242, 75, 23);
		panel.add(cancel);

	}

	public JFrame getThis()
	{
		return this;
	}
}
